<?php

namespace App\Http\Requests;

use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class HpiStoreRequest extends FormRequest
{
    protected $drug;

    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Prepare the data for validation.
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->drug = app('field')->get('drug');
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'record_id' => [
                'required', 'integer', 'min:1',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                    $user = \Auth::user();
                    if (!$user->isDoctorsAdmin() && !$user->isAdmin()) {
                        $query->where(['doctor_id' => $user->id]);
                    }
                }),
            ],

            'diet'           => 'nullable|array',
            'diet.staple'    => 'numeric|min:0|max:999|decimal:1',
            'diet.meat'      => 'in:1,2,3',
            'diet.fat'       => 'in:1,2,3',
            'diet.vegetable' => 'in:1,2,3',
            'diet.fruit'     => 'in:1,2,3',
            'diet.salt'      => 'in:1,2,3',
            'diet.breakfastAdditional'      => 'numeric|min:0|max:999|decimal:1',
            'diet.breakfast'      => 'numeric|min:0|max:999|decimal:1',
            'diet.lunchAdditional'      => 'numeric|min:0|max:999|decimal:1',
            'diet.lunch'      => 'numeric|min:0|max:999|decimal:1',
            'diet.dinnerAdditional'      => 'numeric|min:0|max:999|decimal:1',
            'diet.dinner'      => 'numeric|min:0|max:999|decimal:1',

            'sport' => 'in:0,1,2,3,4,5',
            'sport_strength' => 'in:0,1,2,3,4,5',
            'sport_time' => 'numeric',

            'normal.diet'           => 'in:1,2,3',
            'normal.sleep'          => 'in:1,2,3',
            'normal.spirit'         => 'in:1,2,3',
            'normal.emotion'        => 'in:1,2,3',
            'normal.shit.value'     => 'in:1,2,3,4,5,6',
            'normal.shit.customFrequency'     => 'numeric|min:0|max:999',
            'normal.shit.frequency' => 'in:1,2,3,4,5,6',
            'normal.shit.remark'    => 'string|max:50',
            'normal.pee.value'      => 'in:0,1,2,3,4,5,6',
            'normal.pee.difficulty'      => 'in:0,1',
            'normal.pee.frequency'  => 'in:0,1,2,3,4,5,6',
            'normal.pee.remark'     => 'string|max:50',
            'normal.sweat.situation'     => 'in:1,2,3,4,5,6',
            'normal.sweat.remark'     => 'string|max:50',


            'remark' => 'nullable|string|max:255',
        ];
    }

    public function messages()
    {
        return [
            'record_id.required' => '缺少病历ID',
            'record_id.integer'  => '病历ID 必须是整数。',
            'record_id.min'      => '病历ID 必须大于等于 :min。',
            'record_id.exists'   => '病历 不存在或无修改该病历的权限。',

            'remark.max' => '备注最大 :max 个字符',
        ];
    }
}
